package com.ncmem.up6.utils;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import java.text.SimpleDateFormat;
import java.util.Date;

public class JsonDateValueProcessor implements JsonValueProcessor {

    public static JsonConfig build(){
        JsonConfig cfg = new JsonConfig();   //JsonConfig是net.sf.json.JsonConfig中的这个，为固定写法
        cfg.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor());
        return cfg;
    }

    private String format ="yyyy-MM-dd HH:mm:ss";

    public Object processArrayValue(Object value, JsonConfig config) {
        return process(value);
    }

    public Object processObjectValue(String key, Object value, JsonConfig config) {
        return process(value);
    }

    private Object process(Object value){

        if(value instanceof Date){
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(value);
        }
        return value == null ? "" : value.toString();
    }
}
